package user

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"gpt-api/framework/ginplus"
	"gpt-api/framework/log"
	"gpt-api/pkg/model"
	"net/http"
	"sort"
	"strconv"
	"strings"
	"time"
)

// IsVip 判断用户是否是会员 1是 0不是
// 会员类型 1周会员 2月会员 3季度会员 4年会员
func (h *Handler) IsVip(c *gin.Context) error {
	var (
		req struct {
			EquipmentId string `json:"equipment_id" binding:"required"`
			AId         int8   `json:"a_id" binding:"required"`
		}

		info   model.Auser
		result struct {
			Code    string `json:"code"`
			Message string `json:"message"`
			Info    model.Auser
		}
		userNum []model.AdLog
	)

	if err := c.Bind(&req); err != nil {
		return ginplus.ReplyInternalError(c, err)
	}
	// 查询用户会员信息
	err := h.db.Model(&model.Auser{}).Where("equipment_id =?", req.EquipmentId).Find(&info).Error
	if err != nil {
		return ginplus.ReplyInternalError(c, err)
	}

	if info.Types == "7" {
		//查询是否还有使用次数
		err := h.db.Model(&model.AdLog{}).Where("equipment_id =? and a_id = ?", req.EquipmentId, req.AId).Find(&userNum).Error
		if err != nil {
			return ginplus.ReplyInternalError(c, err)
		}
		if len(userNum) == 0 {
			result.Code = "0"
			result.Message = "0"
			result.Info = info
			return ginplus.ReplyJSON(c, result)
		} else {

			result.Code = "1"
			result.Message = "请充值"
			result.Info = info
			return ginplus.ReplyJSON(c, result)
		}

	}
	// 判断是否过期
	if info.ExTime > time.Now().Unix() {
		result.Code = "0"
		result.Message = "0"
		result.Info = info
		return ginplus.ReplyJSON(c, result)

	} else {

		result.Code = "1"
		result.Message = "请充值"
		result.Info = info

		return ginplus.ReplyJSON(c, result)
	}
	return nil
}

// AddLogs 增加用户操作日志
func (h *Handler) AddLogs(c *gin.Context) error {

	var (
		req struct {
			EquipmentId string `json:"equipment_id" binding:"required"`
			AId         int8   `json:"a_id" binding:"required"`
		}
		info model.AdLog
	)

	if err := c.Bind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"message": "pong",
		})
	}
	info.EquipmentId = req.EquipmentId
	info.AId = req.AId

	err := h.db.Model(&model.AdLog{}).Create(&info).Error
	if err != nil {
		return ginplus.ReplyInternalError(c, err)
	}

	return ginplus.ReplyJSON(c, error(nil))
}

// AddUser 增加用户信息
func (h *Handler) AddUser(c *gin.Context) error {

	var (
		req struct {
			EquipmentId string `json:"equipment_id" binding:"required"`
		}
		info model.Auser
	)

	if err := c.Bind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"message": "pong",
		})

	}

	info.EquipmentId = req.EquipmentId
	info.CreateTime = time.Now().Unix()
	info.Types = "7" // 默认为普通用户
	log.Info("ssssss", log.Var("s", info))
	err := h.db.Model(&model.Auser{}).Create(&info).Error
	if err != nil {
		return ginplus.ReplyInternalError(c, err)
	}

	return ginplus.ReplyJSON(c, error(nil))
}

// GetResult 数据库操作//待开奖
func (h *Handler) GetResult(c *gin.Context) error {

	var (
		info []model.Encourage

		open model.Encourage
	)

	err := h.db.Model(&model.Encourage{}).Where("status = ?", 1).Or("status =?", 2).Order("expect desc").Limit(1).Find(&info).Error

	if err != nil {
		return ginplus.ReplyInternalError(c, err)
	}

	err = h.db.Model(&model.Encourage{}).Where("status = ?", 3).Order("expect desc").Limit(1).Find(&open).Error

	if err != nil {
		return ginplus.ReplyInternalError(c, err)
	}
	if open.ID != 0 {
		info = append(info, open)
	}

	return ginplus.ReplyJSON(c, info)
}

// GetResultList 数据库操作 列表接口
func (h *Handler) GetResultList(c *gin.Context) error {

	var (
		info []model.Encourage
	)

	err := h.db.Model(&model.Encourage{}).Where("status = ?", 2).Order("expect desc").Find(&info).Error
	if err != nil {
		return ginplus.ReplyInternalError(c, err)
	}

	return ginplus.ReplyJSON(c, info)
}

// GetResultSoon 即将开奖的是哪一期数，//返回期数//即将开奖的时间
func (h *Handler) GetResultSoon(c *gin.Context) error {

	var (
		info []model.Encourage
	)

	err := h.db.Model(&model.Encourage{}).Where("status = ?", 3).Find(&info).Error
	if err != nil {
		return ginplus.ReplyInternalError(c, err)
	}

	return ginplus.ReplyJSON(c, info)
}

// GetResultDoll 最近的五期开奖结果
func (h *Handler) GetResultDoll(c *gin.Context) error {

	var (
		info []model.Encourage
	)

	err := h.db.Model(&model.Encourage{}).Where("status = ?", 2).Order("expect desc").Limit(5).Find(&info).Error
	if err != nil {
		return ginplus.ReplyInternalError(c, err)
	}

	return ginplus.ReplyJSON(c, info)
}

// 获取料数据

func (h *Handler) GetStrategy(c *gin.Context) error {

	var (
		req struct {
			Ptype int8 `json:"ptype" binding:"required"`
		}
		info []model.Prediction
	)
	if err := c.Bind(&req); err != nil {
		return ginplus.ReplyInternalError(c, err)

	}

	err := h.db.Model(&model.Prediction{}).Where("ptype = ?", req.Ptype).Order("expect desc").Find(&info).Error
	if err != nil {
		return ginplus.ReplyInternalError(c, err)
	}

	return ginplus.ReplyJSON(c, info)
}

func (h *Handler) GetStrategyH(c *gin.Context) error {

	var (
		req struct {
			Ptype int8 `json:"ptype" binding:"required"`
		}
		info  []model.Prediction
		info2 []model.Prediction
	)
	if err := c.Bind(&req); err != nil {
		return ginplus.ReplyInternalError(c, err)

	}
	//获取当前最新的期数
	var expect model.Encourage
	err := h.db.Model(&model.Encourage{}).Where("status = ?", 3).Find(&expect).Error
	if err != nil {
		return ginplus.ReplyInternalError(c, err)
	}
	//根据最新期数查出最新三期的料
	err = h.db.Model(&model.Prediction{}).Where("ptype = ?", req.Ptype).Where("expect <= ?", expect.Expect).Order("expect desc").Limit(3).Find(&info).Error
	if err != nil {
		return ginplus.ReplyInternalError(c, err)
	}
	//根据之前查出的料获取倒数第三期的期号

	err = h.db.Model(&model.Prediction{}).Where("ptype = ?", req.Ptype).Where("win = ?", 1).Where("expect < ?", info[2].Expect).Order("expect desc").Find(&info2).Error
	if err != nil {
		return ginplus.ReplyInternalError(c, err)
	}

	//把info和info2的数据合并
	info = append(info, info2...)

	return ginplus.ReplyJSON(c, info)
}

// GetImage 获取当天的要展示的图片
func (h *Handler) GetImage(c *gin.Context) error {

	var (
		req struct {
			Ptype int8 `json:"ptype" binding:"required"`
		}
		info []model.Images
	)
	if err := c.Bind(&req); err != nil {
		return ginplus.ReplyInternalError(c, err)
	}

	err := h.db.Model(&model.Images{}).Where("status = ?", 1).Where("ptype = ?", req.Ptype).Find(&info).Error
	if err != nil {
		return ginplus.ReplyInternalError(c, err)
	}

	return ginplus.ReplyJSON(c, info)
}

func containsString(zodiacs []string, zodiac string) bool {
	for _, item := range zodiacs {
		if item == zodiac {
			return true
		}
	}

	return false
}

// 根据传入的六合彩的中奖号码获取特码，并换算出特码对应的生肖，对比正六肖料、正七肖料、正八肖料、正九肖料、正十肖料 判断是否中，中了返回true，并返回生肖。没中返回false
func isZodiacPredictionHit(zodiacPrediction, specialZodiacs string) (bool, string) {
	//获取特码
	specialCode := generateSpecialCode(zodiacPrediction)
	//specialCode是一个字符串，需要转换成int
	specialCodeInt, _ := strconv.Atoi(specialCode)
	//获取特码对应的生肖
	specialZodiac := GetZodiac(specialCodeInt)
	//判断特码对应的生肖是否在正几肖料中
	if containsString(strings.Split(specialZodiacs, ","), specialZodiac) {
		return true, specialZodiac
	}

	return false, ""
}

// 判断特码是否中去几尾料，中了返回false，没中返回true
func isKillTailPredictionHit(zodiacPrediction, Predictions string) (bool, string) {
	specialCode := generateSpecialCode(zodiacPrediction)
	//获取特码对应的尾数
	specialTail := specialCode[len(specialCode)-1:]
	//转换成int
	//Predictions是一个字符串，需要转换成数组
	predictions := strings.Split(Predictions, ",")
	//判断特码对应的尾数是否在去几尾料中
	if containsString(predictions, specialTail) {
		return false, specialCode
	}

	return true, specialCode
}

// 判断特码是否中去几肖料，中了返回false，没中返回true
func isKillZodiacPredictionHit(zodiacPrediction, Predictions string) (bool, string) {
	//获取特码
	specialCode := generateSpecialCode(zodiacPrediction)
	//转换成int
	specialCodeInt, _ := strconv.Atoi(specialCode)
	//获取特码对应的生肖
	specialZodiac := GetZodiac(specialCodeInt)
	//Predictions是一个字符串，需要转换成数组
	predictions := strings.Split(Predictions, ",")
	//判断特码对应的生肖是否在去几肖料中
	if containsString(predictions, specialZodiac) {
		return false, specialZodiac
	}

	return true, specialZodiac
}

// 判断特码是否中正几码料，中了返回true，没中返回false
func isZodiacCodePredictionHit(zodiacPrediction, Predictions string) (bool, string) {
	//获取特码
	specialCode := generateSpecialCode(zodiacPrediction)
	//Predictions是一个字符串，需要转换成数组
	predictions := strings.Split(Predictions, ",")
	//判断特码是否在正几码料中
	if containsString(predictions, specialCode) {
		return true, specialCode
	}

	return false, specialCode
}

// 判断特码是否中正几尾料，中了返回true,中的哪个，没中返回false
func isZodiacTailPredictionHit(zodiacPrediction, Predictions string) (bool, string) {
	//获取特码
	specialCode := generateSpecialCode(zodiacPrediction)
	//获取特码的尾数，并转化成01,02,03,04,05,06,07,08,09格式
	specialCodeTail, _ := strconv.Atoi(specialCode[1:])
	specialCodeTails := fmt.Sprintf("%02d", specialCodeTail)
	//Predictions是一个字符串，需要转换成数组
	predictions := strings.Split(Predictions, ",")
	//判断特码是否在正几尾料中
	if containsString(predictions, specialCodeTails) {
		return true, specialCode[1:]
	}

	return false, ""
}

// 判断特码是否中三色波料，中了返回true，并返回那个颜色，没中返回false
func isThreeColorPredictionHit(zodiacPrediction, Predictions string) (bool, string) {
	//获取特码
	specialCode := generateSpecialCode(zodiacPrediction)
	//字符串类型，需要转换成int类型
	specialCodeInt, _ := strconv.Atoi(specialCode)
	color := GetColor(specialCodeInt)

	fmt.Print("color:", color, "\n")
	fmt.Print("Predictions:", Predictions, "\n")
	if color == Predictions {
		return true, color
	}

	return false, ""
}

// 判断特码是否中三色波料，中了返回true，并返回那个颜色，没中返回false
func isThreeColorDoublePredictionHit(zodiacPrediction, Predictions string) (bool, string) {
	//获取特码
	specialCode := generateSpecialCode(zodiacPrediction)
	//字符串类型，需要转换成int类型
	specialCodeInt, _ := strconv.Atoi(specialCode)
	color := GetColor(specialCodeInt)

	fmt.Print("color:", color, "\n")
	fmt.Print("Predictions:", Predictions, "\n")
	if strings.Contains(Predictions, color) {
		return true, color
	}

	return false, ""
}

// 判断特码是否中平特一肖、平特二肖，中了返回true，并返回那个生肖，没中返回false
func isptxPredictionHit(zodiacPrediction, Predictions string) (bool, string) {
	//zodiacPrediction是一个字符串，需要转换成数组
	zodiacPredictions := strings.Split(zodiacPrediction, ",")
	specialCode := GetZodiacs(zodiacPredictions)
	fmt.Print("specialCode:", specialCode, "\n")
	zodiac := strings.Split(Predictions, ",")
	//判断specialCode中是否包含zodiac
	for _, v := range zodiac {
		//判断字符串中是否包含另一个字符串
		if strings.Contains(specialCode, v) {
			return true, v
		}

	}

	return false, ""
}

// isptwPredictionHit 判断特码是否中平特一尾、平特二尾，中了返回true，并返回那个尾数，没中返回false
func isptwPredictionHit(zodiacPrediction, Predictions string) (bool, string) {
	//zodiacPrediction是一个字符串，需要转换成数组
	zodiacPredictions := strings.Split(zodiacPrediction, ",")
	//获取这些号码的尾号
	specialCode := GetTail(zodiacPredictions)
	fmt.Print("specialCode:", specialCode, "\n")
	zodiac := strings.Split(Predictions, ",")

	//判断字符串中是否包含另一个字符串
	for _, v := range zodiac {
		if strings.Contains(specialCode.(string), v) {
			return true, v
		}

	}

	return false, ""
}

func GetTail(predictions []string) interface{} {
	var result []string
	for _, v := range predictions {
		//字符串类型，需要转换成int类型
		specialCodeInt, _ := strconv.Atoi(v)
		//获取尾数
		tail := specialCodeInt % 10
		result = append(result, strconv.Itoa(tail))
	}
	return strings.Join(result, ",")
}

// 判断generateSpecialZodiacPrediction是否中
func isSpecialZodiacPrediction(OpenCode string, zodiac string) (bool, string) {
	//获取特码
	ep := generateSpecialCode(OpenCode)
	//将字符串转换为int
	exp, _ := strconv.Atoi(ep)
	//获取特码对应的生肖
	GetZodiac := GetZodiac(exp)
	//判断特码对应的生肖是否在generateSpecialZodiacPrediction中
	if strings.Contains(zodiac, GetZodiac) {
		return true, GetZodiac
	}
	return false, ""

}

// 判断generateThreeXingPrediction是否中
func isThreeXingPrediction(OpenCode string, threeXingPrediction string) (bool, string) {
	//把OpenCode转化成字符串数组
	OpenCodeArr := strings.Split(OpenCode, ",")

	ep := GetZodiacs(OpenCodeArr)
	//把threeXingPrediction以空格分割成字符串数组
	threeXingPredictionArr := strings.Split(threeXingPrediction, " ")
	//判断OpenCodeArr是否包含threeXingPredictionArr
	if containsStringArr(ep, threeXingPredictionArr) {
		return true, ""

	}
	return false, ""
}

func containsStringArr(ep string, arr []string) bool {
	//遍历arr
	for _, v := range arr {
		//判断ep是否包含v
		//  把v转化成字符串数组
		vArr := strings.Split(v, ",")
		//判断ep是否包含vArr中的三个值，如果全包含就返回true
		if containsStringArr2(ep, vArr) {
			return true
		}
	}
	return false
}

// 判断containsStringArr是否包含
func containsStringArr2(ep string, arr []string) bool {
	//遍历arr
	count := 0
	for _, v := range arr {
		//arr 中三个值都包含在ep中，就返回true
		if strings.Contains(ep, v) {
			count++
		}
	}
	if count == 3 {
		return true
	}
	return false
}

// 判断三行必中有没有中
func isThreeXingPrediction2(OpenCode string, threeXingPrediction string) (bool, string) {

	//获取特码
	ep := generateSpecialCode(OpenCode)
	//ep转化成int类型
	epInt, _ := strconv.Atoi(ep)

	//获取特码对应的生肖
	wx := GetMaWxByNumber(epInt)

	threeXingPredictionArr := strings.Split(threeXingPrediction, ",")
	//判断OpenCodeArr是否包含threeXingPredictionArr
	for _, v := range threeXingPredictionArr {
		if strings.Contains(wx, v) {
			return true, v
		}
	}
	return false, ""
}

// 判断平特六码二中二有没有中 //num 为中几个,中1，中二，中三
func isSixCodePrediction(OpenCode string, sixCodePrediction string, num int) (bool, string) {

	//sixCodePrediction转化成字符串数组
	sixCode := strings.Split(sixCodePrediction, ",")
	//sixCodePrediction转化成字符串数组
	//判断OpenCode是否包含sixCode
	//定义记数器
	count := 0
	//遍历sixCode
	for _, v := range sixCode {

		//判断OpenCode是否包含v
		if strings.Contains(OpenCode, v) {
			count++
		}
	}

	if count >= num {
		return true, sixCodePrediction
	}
	return false, ""
}

func (h *Handler) ImitateCalculatingAward(c *gin.Context) error {
	var (
		req struct {
			Code   string `form:"code"`
			Expect string `form:"expect"`
		}
	)
	if err := c.Bind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"message": "pong",
		})

	}

	var prediction []model.Prediction
	var pred []model.Prediction

	//查询数据库中的数据,查询期数最大的状态为2的数据
	h.db.Debug().Where("expect = ?", req.Expect).Find(&prediction)
	//把执行的gorm语句打印出来

	//判断几肖是否命中
	code := req.Code

	//根据ptype一共1-18种类型，1-5：正几肖料，6-9：去几尾，10-11：去几肖，12-14：正几码，15-17：正几尾，18：三色波料，生成switch case

	for _, item := range prediction {
		//初始化静态变量
		var zodiacs bool
		var win int8
		switch item.Ptype {
		case 1, 2, 3, 4, 5:
			zodiacs, _ = isZodiacPredictionHit(code, item.Zodiac)
		case 6, 7, 8, 9:
			zodiacs, _ = isKillTailPredictionHit(code, item.Zodiac)
		case 10, 11:
			zodiacs, _ = isKillZodiacPredictionHit(code, item.Zodiac)
		case 12, 13, 14:
			zodiacs, _ = isZodiacCodePredictionHit(code, item.Zodiac)
		case 15, 16, 17:
			zodiacs, _ = isZodiacTailPredictionHit(code, item.Zodiac)
		case 18:
			zodiacs, _ = isThreeColorPredictionHit(code, item.Zodiac)
		case 19:
			zodiacs, _ = isThreeColorDoublePredictionHit(code, item.Zodiac)
		case 20, 21:
			zodiacs, _ = isptxPredictionHit(code, item.Zodiac)
		case 22, 23:
			zodiacs, _ = isptwPredictionHit(code, item.Zodiac)
		case 26, 27, 28, 29, 30, 31, 32, 33, 34:
			zodiacs, _ = isSpecialZodiacPrediction(code, item.Zodiac)
		case 35:
			zodiacs, _ = isThreeXingPrediction(code, item.Zodiac)
		case 36:
			zodiacs, _ = isThreeXingPrediction2(code, item.Zodiac)
		case 37:
			zodiacs, _ = isSixCodePrediction(code, item.Zodiac, 1)
		case 38:
			zodiacs, _ = isSixCodePrediction(code, item.Zodiac, 2)
		case 39:
			zodiacs, _ = isSixCodePrediction(code, item.Zodiac, 3)

		}

		fmt.Print("item.Zodiac:", item, "\n")
		if zodiacs {
			win = 1
		} else {
			win = 2
		}

		item.Win = win
		item.Opencode = code
		pred = append(pred, item)

	}
	return ginplus.ReplyJSON(c, pred)
}

func (h *Handler) ImitateList(c *gin.Context) error {

	var prediction []model.Prediction
	var pr model.Encourage

	//查询数据库中的数据,查询期数最大的状态为2的数据
	h.db.Debug().Where("status= ?", 3).Order("expect desc").Find(&pr)
	h.db.Debug().Where("expect = ?", pr.Expect).Find(&prediction)
	//把执行的gorm语句打印出来

	//判断几肖是否命中

	//根据ptype一共1-18种类型，1-5：正几肖料，6-9：去几尾，10-11：去几肖，12-14：正几码，15-17：正几尾，18：三色波料，生成switch case

	return ginplus.ReplyJSON(c, prediction)
}

// GetExpect 获取期数号码
func (h *Handler) GetExpect(c *gin.Context) error {
	var (
		req struct {
			Expect string `form:"expect"`
		}
	)
	if err := c.Bind(&req); err != nil {
		return err
	}
	var prediction []model.Encourage
	//查询数据库中的数据,查询期数最大的状态为2的数据
	h.db.Debug().Select("expect").Where("status != ?", 4).Order("expect desc").Find(&prediction)

	return ginplus.ReplyJSON(c, prediction)
}

// CalculatingAward 摇奖算法开始
func (h *Handler) CalculatingAward(c *gin.Context) error {
	var (
		info []model.Encourage

		open model.Encourage
	)

	err := h.db.Model(&model.Encourage{}).Where("status = ?", 1).Or("status =?", 2).Order("expect desc").Limit(1).Find(&info).Error

	if err != nil {
		return ginplus.ReplyInternalError(c, err)
	}
	if info[0].Status == 1 {

		var times int64
		times = info[0].Opentime
		//获取开奖号码
		opens := info[0].Opencode
		hk := []string{
			"正",
			"在",
			"进",
			"行",
			"搅",
			"珠",
			"中",
		}
		//把开奖号码转化成数组
		code := strings.Split(opens, ",")
		//以一分钟时间戳为基准，获取当前时间戳
		timestamp := time.Now().Unix()
		//把一分钟时间长度等分成7份
		//每份的时间长度

		st := timestamp - times
		sp := st / 8

		var sy []string
		switch sp {
		case 0:
			sy = append(sy, hk[0:7]...)
		case 1:
			sy = code[0:1]
			sy = append(sy, hk[1:]...)
		case 2:
			sy = code[0:2]
			sy = append(sy, hk[2:]...)
		case 3:
			sy = code[0:3]
			sy = append(sy, hk[3:]...)
		case 4:
			sy = code[0:4]
			sy = append(sy, hk[4:]...)
		case 5:
			sy = code[0:5]
			sy = append(sy, hk[5:]...)
		case 6:
			sy = code[0:6]
			sy = append(sy, hk[6:]...)
		case 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25:
			sy = code[0:7]
			sy = append(sy, hk[7:]...)
		default:
			sy = append(sy, hk[0:7]...)
		}
		info[0].Opencode = strings.Join(sy, ",")

	}

	err = h.db.Model(&model.Encourage{}).Where("status = ?", 3).Order("expect desc").Limit(1).Find(&open).Error

	if err != nil {
		return ginplus.ReplyInternalError(c, err)
	}
	if open.ID != 0 {
		info = append(info, open)
	}

	return ginplus.ReplyJSON(c, info)
}

// 设置map key,value 为1：1
var maPrediction = map[int8]int8{
	1:  1,
	2:  2,
	3:  3,
	4:  4,
	5:  5,
	10: 10,
	11: 11,
	20: 20,
	21: 21,
	26: 26,
	27: 27,
	28: 28,
	29: 29,
	30: 30,
	31: 31,
	32: 32,
	33: 33,
	34: 34,
	35: 35,
	40: 40,
	41: 41,
	42: 42,
}

// 为特码初始化一个字符串数组
var strPrediction = []int8{
	1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 26, 27, 28, 29, 30, 31, 32, 33, 34, 36, 40, 41, 42,
}

// pingDiction 为平码初始化一个字符串数组
var pingDiction = []int8{
	20, 21, 22, 23, 37, 38, 39,
}

// 为热门初始化一个字符串数组
var hotDiction = []int8{
	19, 26, 27, 28, 29,
}

// 为热门平初始化一个字符串数组
var hotPingDiction = []int8{
	20, 21, 22, 38, 39,
}

// CalculateProbability 本方法用于计算在一期数字的中奖概率是多少
func (h *Handler) CalculateProbability(c *gin.Context) error {
	var (
		req struct {
			Expect string `form:"expect"`
		}
		//定义一个结构体，用于存储要返回的高中低的数据
		rsp struct {
			High string `json:"high"`
			Mid  string `json:"mid"`
			Low  string `json:"low"`
		}
		prediction []model.Prediction
	)
	if err := c.Bind(&req); err != nil {
		return ginplus.ReplyInternalError(c, err)
	}

	//查询数据库中的数据,查询期数Expect的全部料的数据
	err := h.db.Debug().Where("expect = ?", req.Expect).Find(&prediction).Error
	if err != nil {
		return ginplus.ReplyInternalError(c, err)
	}
	//初始化一个map，用于存储每个料的数字(原始数据)
	var numMap = make(map[int8][]string)

	for _, item := range prediction {
		//判断字符串是否在map中存在
		if _, ok := maPrediction[item.Ptype]; ok {
			if item.Ptype == 35 {
				//将字符串中的空格替换成,
				item.Zodiac = strings.Replace(item.Zodiac, " ", ",", -1)
			}
			//跟据生肖的类型，把生肖的数字放到map中
			sx := GetZodiacNumbers(item.Zodiac)
			numMap[item.Ptype] = append(numMap[item.Ptype], sx...)
		} else {
			//将Zodiac转化成字符串数字
			if item.Ptype != 36 && item.Ptype != 19 && item.Ptype != 18 {
				zb := strings.Split(item.Zodiac, ",")
				//当字符串中包含01,02,03,04,05,06,07,08,09的时候，去掉前面的0
				for i, v := range zb {
					if strings.Contains(v, "0") {
						zb[i] = strings.TrimLeft(v, "0")
					}
				}
				numMap[item.Ptype] = append(numMap[item.Ptype], zb...)
			} else {
				if item.Ptype == 18 || item.Ptype == 19 {
					zbs := strings.Split(item.Zodiac, ",")
					//根据颜色获取对应的数字
					for _, v := range zbs {
						np := GetMaColorByColor(v)
						numMap[item.Ptype] = append(numMap[item.Ptype], np...)
					}
				}
			}
		}

	}

	//初始化一个map，用于存储绝杀的数字
	var killMap = make(map[int64]int64)
	//循环获取numMap中的key值6-11的数字
	for k, v := range numMap {
		if k >= 6 && k <= 11 {
			for _, v1 := range v {
				//将字符串转化成int8类型而不是int64类型
				v2, _ := strconv.ParseInt(v1, 10, 8)
				//判段killMap中是否存在v2的key值，如果存在，将v1添加到killMap中的v2的value中
				if _, ok := killMap[v2]; ok {
					killMap[v2] = killMap[v2] + 1

				} else {
					killMap[v2] = 1
				}

			}
		}
	}
	//循环获取numMap中的key其他的平特数字的值
	var teMap = make(map[int64]int64)
	for k, v := range numMap {
		if k != 6 && k != 7 && k != 8 && k != 9 && k != 10 && k != 11 {
			for _, v1 := range v {
				//将字符串转化成int8类型而不是int64类型
				v2, _ := strconv.ParseInt(v1, 10, 8)
				if _, ok := teMap[v2]; ok {
					teMap[v2] = teMap[v2] + 1

				} else {
					teMap[v2] = 1
				}
			}
		}
	}
	//从teMap中删除killMap中的key
	for k, _ := range killMap {
		delete(teMap, k)
	}

	//循环获取numMap中的key为strPrediction数组的特码
	var zTeMap = make(map[int64]int64)
	for _, v := range strPrediction {
		for _, v1 := range numMap[v] {
			//将字符串转化成int8类型而不是int64类型
			v2, _ := strconv.ParseInt(v1, 10, 8)
			if _, ok := zTeMap[v2]; ok {
				zTeMap[v2] = zTeMap[v2] + 1

			} else {
				zTeMap[v2] = 1
			}

		}
	}
	//从zTeMap中删除killMap中的key
	//for k, _ := range killMap {
	//	delete(zTeMap, k)
	//}

	//把zTeMap中的key,value转化成切片
	var zTeSlice []Kill
	for k, v := range zTeMap {
		zTeSlice = append(zTeSlice, Kill{Number: k, Count: v})
	}
	//对zTeSlice进行排序
	sort.Slice(zTeSlice, func(i, j int) bool {
		return zTeSlice[i].Count > zTeSlice[j].Count
	})

	//循环获取numMap中的key为pingDiction数组的平码
	var pingMap = make(map[int64]int64)
	for _, v := range pingDiction {
		for _, v1 := range numMap[v] {
			//将字符串转化成int8类型而不是int64类型
			v2, _ := strconv.ParseInt(v1, 10, 8)
			if _, ok := pingMap[v2]; ok {
				pingMap[v2] = pingMap[v2] + 1

			} else {
				pingMap[v2] = 1
			}

		}
	}
	//从pingMap中删除killMap中的key
	//for k, _ := range killMap {
	//	delete(pingMap, k)
	//}
	//把pingMap中的key,value转化成切片
	var pingSlice []Kill
	for k, v := range pingMap {
		pingSlice = append(pingSlice, Kill{Number: k, Count: v})
	}
	//对pingSlice进行排序
	sort.Slice(pingSlice, func(i, j int) bool {
		return pingSlice[i].Count > pingSlice[j].Count
	})

	//循环获取numMap中的key为hotDiction数组的特码
	var hotMap = make(map[int64]int64)
	for _, v := range hotDiction {
		for _, v1 := range numMap[v] {
			//将字符串转化成int8类型而不是int64类型
			v2, _ := strconv.ParseInt(v1, 10, 8)
			if _, ok := hotMap[v2]; ok {
				hotMap[v2] = hotMap[v2] + 1

			} else {
				hotMap[v2] = 1
			}

		}
	}
	//从hotMap中删除killMap中的key
	for k, _ := range killMap {
		delete(hotMap, k)
	}
	//把hotMap中的key,value转化成切片
	var hotSlice []Kill
	for k, v := range hotMap {
		hotSlice = append(hotSlice, Kill{Number: k, Count: v})
	}
	//对hotSlice进行排序
	sort.Slice(hotSlice, func(i, j int) bool {
		return hotSlice[i].Count > hotSlice[j].Count
	})

	//循环获取numMap中的key为hotPingDiction数组的平码
	var hotPingMaps = make(map[int64]int64)
	for _, v := range hotPingDiction {
		for _, v1 := range numMap[v] {
			//将字符串转化成int8类型而不是int64类型
			v2, _ := strconv.ParseInt(v1, 10, 8)
			if _, ok := hotPingMaps[v2]; ok {
				hotPingMaps[v2] = hotPingMaps[v2] + 1

			} else {
				hotPingMaps[v2] = 1
			}

		}
	}
	//从hotPingMaps中删除killMap中的key
	for k, _ := range killMap {
		delete(hotPingMaps, k)

	}
	//把hotPingMaps中的key,value转化成切片
	var hotPingSlice []Kill
	for k, v := range hotPingMaps {
		hotPingSlice = append(hotPingSlice, Kill{Number: k, Count: v})

	}
	//对hotPingSlice进行排序
	sort.Slice(hotPingSlice, func(i, j int) bool {
		return hotPingSlice[i].Count > hotPingSlice[j].Count
	})

	//把killMap中的key,value转化成切片
	var killSlice []Kill
	for k, v := range killMap {
		killSlice = append(killSlice, Kill{Number: k, Count: v})
	}
	//对killSlice进行排序
	sort.Slice(killSlice, func(i, j int) bool {
		return killSlice[i].Count > killSlice[j].Count
	})
	//把teMap中的key,value转化成切片
	var teSlice []Kill
	for k, v := range teMap {
		teSlice = append(teSlice, Kill{Number: k, Count: v})
	}
	//对teSlice进行排序
	sort.Slice(teSlice, func(i, j int) bool {
		return teSlice[i].Count > teSlice[j].Count
	})

	//cpoy teMap的值
	var teMapCopy = make(map[int64]int64)
	for k, v := range teMap {
		teMapCopy[k] = v
	}
	//从teMapCopy中删除killSlice中的key
	for _, v := range killSlice {
		delete(teMapCopy, v.Number)
	}
	//把teMapCopy中的key,value转化成切片
	var teSliceCopy []Kill
	for k, v := range teMapCopy {
		teSliceCopy = append(teSliceCopy, Kill{Number: k, Count: v})
	}
	//对teSliceCopy进行排序
	sort.Slice(teSliceCopy, func(i, j int) bool {
		return teSliceCopy[i].Count > teSliceCopy[j].Count
	})
	fmt.Print("teSliceCopy:", teSliceCopy, "\n")
	//从teSliceCopy中取出前前七条数据第一条放到最后一位，其他打乱顺序
	var teSlices []string
	for i, v := range teSliceCopy {
		teSlices = append(teSlices, strconv.FormatInt(v.Number, 10))
		if i >= 6 {
			break
		}
	}
	//把teSlices中的第一条数据放到最后一位，其他打乱顺序
	teSlices = append(teSlices[1:], teSlices[0])
	a := numMap[20]
	fmt.Print("a:", a, "\n")
	fmt.Print("teSlices:", teSlices, "\n")

	//获取zTeSlice的第一位数据，放到最后一位，获取pingSlice的前六位放到新数组的前六位
	var hTeSlices []string
	hTeSlices = append(hTeSlices, strconv.FormatInt(zTeSlice[0].Number, 10))
	for i, v := range pingSlice {
		hTeSlices = append(hTeSlices, strconv.FormatInt(v.Number, 10))
		if i >= 5 {
			break
		}
	}
	//把zTeSlices中的第一条数据放到最后一位，其他打乱顺序
	hTeSlices = append(hTeSlices[1:], hTeSlices[0])

	//获取zTeSlice中间的一条数据放到最后一位，获取pingSlice的中间六位放到新数组的前六位
	var mTeSlices []string
	num := len(zTeSlice)
	mTeSlices = append(mTeSlices, strconv.FormatInt(zTeSlice[num/2].Number, 10))
	//从pingSlice中取出中间六条数据
	pi := len(pingSlice)

	//判断pingSlice的长度是否大于6,如果大于6则取出中间六条数据，否则取出全部数据
	if pi > 6 {
		for i, v := range pingSlice {
			if i <= 4 {
				continue
			}
			mTeSlices = append(mTeSlices, strconv.FormatInt(v.Number, 10))
			if i >= 10 {
				break
			}
		}
	} else {
		for _, v := range pingSlice {
			mTeSlices = append(mTeSlices, strconv.FormatInt(v.Number, 10))
		}
	}
	//把mTeSlices中的第一条数据放到最后一位，其他打乱顺序
	mTeSlices = append(mTeSlices[1:], mTeSlices[0])

	//获取zTeSlice中间的一条数据放到最后一位，获取pingSlice的中间六位放到新数组的前六位
	var lTeSlices []string
	lTeSlices = append(lTeSlices, strconv.FormatInt(zTeSlice[num-1].Number, 10))
	fmt.Print("111111111111111lTeSlices:", lTeSlices, "\n")
	//从pingSlice中取出后六条数据
	//判断pingSlice的长度是否大于6,如果大于6则取出后六条数据，否则取出全部数据
	if pi > 6 {
		for i, v := range pingSlice {
			if i <= pi-7 {
				continue
			}
			lTeSlices = append(lTeSlices, strconv.FormatInt(v.Number, 10))

		}
	} else {
		for _, v := range pingSlice {
			lTeSlices = append(lTeSlices, strconv.FormatInt(v.Number, 10))
		}
	}
	//把lTeSlices中的第一条数据放到最后一位，其他打乱顺序
	lTeSlices = append(lTeSlices[1:], lTeSlices[0])

	//把rsp中的数据其中个1，2，3，4，5，6，7，8，9变换成01，02，03，04，05，06，07，08，09
	var highSlice []string
	var midSlice []string
	var lowSlice []string
	highSlice = teSlices
	midSlice = mTeSlices
	lowSlice = lTeSlices
	for i, v := range highSlice {
		if len(v) == 1 {
			highSlice[i] = "0" + v
		}

	}
	for i, v := range midSlice {
		if len(v) == 1 {
			midSlice[i] = "0" + v
		}

	}
	for i, v := range lowSlice {
		if len(v) == 1 {
			lowSlice[i] = "0" + v
		}

	}
	rsp.High = strings.Join(highSlice, ",")
	rsp.Mid = strings.Join(midSlice, ",")
	rsp.Low = strings.Join(lowSlice, ",")

	return ginplus.ReplyJSON(c, rsp)

}

type Kill struct {
	Number int64
	Count  int64
}
